//
//  HomeVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/4.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "HomeVC.h"
#import "CenterVC.h"
#import "TestCenterVC.h"
#import "SignUpVC.h"
#import "AliefuGymVC.h"
#import "StudyCenterVC.h"
#import "AdvertModel.h"
#import "LoginVC.h"
#import "homeCollectionCell.h"
#import "niceCollectionCell.h"
#import "CenterVC.h"
#import "NewSetVC.h"
#import "StudyVC.h"

@interface HomeVC ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIImageView *user_img;
@property (strong, nonatomic) IBOutlet UIButton *nameBtn;
@property (strong, nonatomic) IBOutlet UICollectionView *left_collectionView;

@property (strong, nonatomic) IBOutlet UIView *top_view;
@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@property (strong, nonatomic) IBOutlet UILabel *title_lab;
@property (strong, nonatomic) IBOutlet UIButton *search_btn;
@property (strong, nonatomic) IBOutlet UIButton *phone_btn;
@property (strong, nonatomic) IBOutlet UIButton *more_btn;
@property (strong, nonatomic) IBOutlet UILabel *class_lab;

@end

@implementation HomeVC{
    RequestServices *service;
    NSMutableArray *imgArr;//轮播图获取数组
    NSArray *icon_Arr;//图标数组
    NSArray *title_Arr;//名称数组
    SDCycleScrollView *cycleScrollView;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.titleView setHidden:YES];
    [self.backKuang setHidden:YES];
    self.backImgView.image = [UIImage imageNamed:@"home_back"];
    self.backImgView.contentMode = UIViewContentModeScaleAspectFill;
    //设置字体适应
    self.title_lab.font = self.class_lab.font = AutoLabFont1;
    self.more_btn.titleLabel.font = AutoLabFont2;
    [self.class_lab setHidden:YES];
    [self.more_btn setHidden:YES];
//    APPViewBorderRadius(self.more_btn, self.more_btn.frame.size.height/2, .1, [UIColor clearColor]);
    //更多
    [self.more_btn addTarget:self action:@selector(pushMore) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *line_img = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W * 0.1, SCREEN_H*0.02, SCREEN_W*0.9, SCREEN_H)];
    line_img.image = [UIImage imageNamed:@"home_border"];
    [self.view insertSubview:line_img atIndex:2];
    
    UIView *line_view = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_W*0.1, SCREEN_H*0.12 + 10, SCREEN_W*0.9, 1)];
    line_view.backgroundColor = Base_lineColor;
    [self.view addSubview:line_view];
    
    self.left_collectionView.delegate = self;
    self.left_collectionView.dataSource = self;
    [self.left_collectionView registerNib:[UINib nibWithNibName:@"homeCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"homeCollectionCell"];
    imgArr = [NSMutableArray array];
    icon_Arr = [NSArray arrayWithObjects:@"home_class",@"home_study",@"home_find",@"",@"home_set",@"home_mine", nil];
    title_Arr = [NSArray arrayWithObjects:@"选课中心",@"学习中心",@"发现",@"",@"设置",@"我的", nil];
    
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    [self.collection_view registerNib:[UINib nibWithNibName:@"niceCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"niceCollectionCell"];
    service = [[RequestServices alloc]initWithDelegate:self];

    /*
     * 轮播图接口
     */
    [service getAdvert:@"2" tag:8000];

    /*
     * 精彩课程接口
     */
    list_arr = [[NSMutableArray alloc]init];
    __weak __typeof(self)weakSelf = self;
    //左划
    self.collection_view.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.collection_view.mj_trailer.hidden = YES;
}

- (void)headerRefresh
{
    service.view = self.view;
    page_index = 1;
    [service getCurriculumList:[NSString stringWithFormat:@"%ld",page_index] categoryId:@"0" recommend:@"1" stageId:@"" tag:8010];
}

- (void)footerRefresh
{
    page_index++;
    [service getCurriculumList:[NSString stringWithFormat:@"%ld",page_index] categoryId:@"0" recommend:@"1" stageId:@"" tag:8011];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    [self.collection_view.mj_trailer endRefreshing];
    if (tag == 8000){
        NSArray *urlArr = [AdvertModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        for (int i = 0; i<urlArr.count; i++) {
            AdvertModel *model = urlArr[i];
            [imgArr addObject:model.advertUrl];
        }
        [self addCollectionView];
        [self headerRefresh];
    }
    if (tag == 8010) {
        list_arr = [CurriculumListModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        [self.class_lab setHidden:NO];
        [self.more_btn setHidden:NO];
    }
    if (tag == 8011){
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
        [list_arr addObjectsFromArray:[CurriculumListModel mj_objectArrayWithKeyValuesArray:tempArr]];
    }
    
    [self.collection_view reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}


#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.left_collectionView) {
        return icon_Arr.count;
    }else{
        return list_arr.count;
    }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.left_collectionView){
        return CGSizeMake(collectionView.frame.size.width, (collectionView.frame.size.height)/6);
    }else{
        return CGSizeMake((self.collection_view.frame.size.width - 30)/3, self.collection_view.frame.size.height);
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.left_collectionView) {
        homeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionCell" forIndexPath:indexPath];
        cell.content_lab.text = title_Arr[indexPath.item];
        cell.icon_img.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",icon_Arr[indexPath.item]]];
        return cell;
    }else{
        niceCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"niceCollectionCell" forIndexPath:indexPath];
        cell.model = list_arr[indexPath.row];
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.left_collectionView) {
        if (indexPath.item == 0) {
            //点击阿列夫赛道
            AliefuGymVC *vc = [[AliefuGymVC alloc]init];
            vc.title = @"阿列夫赛道";
            [self PushToViewController:vc];
        }else if (indexPath.item == 1){
            //学习中心
//            StudyCenterVC *vc = [[StudyCenterVC alloc]init];
            StudyVC *vc = [[StudyVC alloc]init];
            [self PushToViewController:vc];
        }else if (indexPath.item == 2){
            //点击实验中心
//            TestCenterVC *vc = [[TestCenterVC alloc]init];
//            [self PushToViewController:vc];
        }else if (indexPath.item ==3){
        }else if (indexPath.item == 4){
            //设置
            NewSetVC *vc = [[NewSetVC alloc]init];
            [self PushToViewController:vc];
        }else{
            //我的
            CenterVC *vc = [[CenterVC alloc]init];
            [self PushToViewController:vc];
        }
    }
    
    if (collectionView == self.collection_view) {
        
    }
    
}

//列间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}


-(void)addCollectionView{
    KJBannerView *banner2 = [[KJBannerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W * 0.9 - 40,SCREEN_H*0.4 )];
    banner2.pageControl.pageType = PageControlStyleCircle;
    banner2.imgCornerRadius = 15;
    banner2.autoScrollTimeInterval = 3;
    banner2.isZoom = YES;
    banner2.itemSpace = -30;
    banner2.itemWidth = (SCREEN_W * 0.9 - 40)*0.6;
    banner2.imageType = KJBannerViewImageTypeMix;
    NSArray *arr = [NSArray arrayWithArray:imgArr];
    NSArray *arr1 = [NSArray arrayWithObjects:@"back_center",@"back_center",@"back_center", nil];
    if (arr.count) {
        banner2.imageDatas = arr;
    }else{
        banner2.imageDatas = arr1;
    }
    [self.top_view addSubview:banner2];
    
    banner2.kSelectBlock = ^(KJBannerView * _Nonnull banner, NSInteger idx) {
        NSLog(@"---------%@,%ld",banner,idx);
    };
    
}


//点击图像
-(void)touchIcon{
    CenterVC *vc = [[CenterVC alloc]init];
    [self PushToViewController:vc];
}

//轮播图消失
- (IBAction)disMiss:(UIButton *)sender {
    [cycleScrollView removeAllSubivews];
}

//登录
-(void)login{
    LoginVC *vc = [[LoginVC alloc]init];
    [self PushToViewController:vc];
}

//搜索
- (IBAction)search:(id)sender {
}

/**
 * 跳转更多
 */
-(void)pushMore{
    StudyVC *vc = [[StudyVC alloc]init];
    [self PushToViewController:vc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
